# -*- coding: utf-8 -*- 
# @Time : 2020-12-06 23:41 
# @Author : derek.zhang 
# @File : forms.py 
# @Software: PyCharm

from django import forms

class RegisterForm(forms.Form):
    username = forms.CharField(required=True,
                               label=u"用户名",
                               error_messages={'required': u'请输入用户名'},
                               widget=forms.TextInput(
                                   attrs={
                                       'placeholder': u'账号',
                                       'row': 1,
                                       'class': 'input-text size -L',
                                   }
                               ),)
    email = forms.CharField(required=True,
                               label=u"邮箱",
                               error_messages={'required': u'请输入邮箱'},
                               widget=forms.TextInput(
                                   attrs={
                                       'placeholder': u'此邮箱用于密码找回',
                                       'row': 1,
                                       'class': 'input-text size -L',
                                   }
                               ), )

    password = forms.CharField(required=True,
                            error_messages={'required': u'请输入密码'},
                            widget=forms.PasswordInput(
                                attrs={
                                    'placeholder': u'密码',
                                    'row': 1,
                                    'class': 'input-text size -L',
                                }
                            ), )
    password2 = forms.CharField(required=True,
                               error_messages={'required': u'请再次输入密码'},
                               widget=forms.PasswordInput(
                                   attrs={
                                       'placeholder': u'确认密码',
                                       'row': 1,
                                       'class': 'input-text size -L',
                                   }
                               ), )

    def pwd_vaildate(self, p1, p2):
        return  p1 == p2

class LoginForm(forms.Form):
    username = forms.CharField(required=True,
                               label=u'用户名',
                               error_messages={'required':u'请输入用户名'},
                               widget=forms.TextInput(
                                   attrs= {
                                       'palceholder': u'账号',
                                       'rows':1,
                                       'class':'input-text size-L'
                                   }
                               ),)
    password = forms.CharField(required=True,
                               label=u'密码',
                               error_messages={'required': u'请输入密码'},
                               widget=forms.PasswordInput(
                                   attrs={
                                       'palceholder': u'密码',
                                       'rows': 1,
                                       'class': 'input-text size-L'
                                   }
                               ), )

class ChangEmailForm(forms.Form):
    new_email = forms.EmailField(required=True,
                               label=u'新邮箱地址',
                               error_messages={'required': u'请输入新邮箱地址'},
                               widget=forms.TextInput(
                                   attrs={
                                       'palceholder': u'新邮箱',
                                       'rows': 1,
                                       'class': 'input-text size-L'
                                   }
                               ), )
    new_email2 = forms.EmailField(required=True,
                                 label=u'新邮箱地址',
                                 error_messages={'required': u'请再次输入新邮箱地址'},
                                 widget=forms.TextInput(
                                     attrs={
                                         'palceholder': u'确认新邮箱地址',
                                         'rows': 1,
                                         'class': 'input-text size-L'
                                     }
                                 ), )

    def claen(self):
        '''
        form 类里方法的运行顺序是 init, clean, validtae（验证字段定义））, svae, 在这里重写类的clean 方法，用于判断在更改邮箱是，
        两次输入的邮箱名是否一模一样，防止用户输入手误，clean 必须返回 cleand_data
        :return:
        '''
        print(self.cleaned_data, '%%%%%%%%%%%%%%%%%%')
        if not self.is_valid():
            raise forms.ValidationError(u'所有项都为必填项')
        elif self.cleaned_data['new_email'] != self.cleaned_data['new_email2']:
            print('******************')
            raise forms.ValidationError(u'两次输入的邮箱地址不一样')
        else:
            cleaned_data = super(ChangEmailForm, self).clean()
        return cleaned_data


class ChangePwdForm(forms.Form):
    oldpassword = forms.CharField(required=True,
                                  label = u'原密码',
                                error_messages={'required': u'请输入原密码'},
                                widget=forms.PasswordInput(
                                    attrs={
                                        'placeholder': u'原密码',
                                        'row': 1,
                                        'class': 'input-text size -L',
                                    }
                                ), )
    newpassword1 = forms.CharField(required=True,
                                  label=u'新密码',
                                  error_messages={'required': u'请输入新密码'},
                                  widget=forms.PasswordInput(
                                      attrs={
                                          'placeholder': u'新密码',
                                          'row': 1,
                                          'class': 'input-text size -L',
                                      }
                                  ), )

    newpassword2 = forms.CharField(required=True,
                                   label=u'确认密码',
                                   error_messages={'required': u'请再次输入新密码'},
                                   widget=forms.PasswordInput(
                                       attrs={
                                           'placeholder': u'确认密码',
                                           'row': 1,
                                           'class': 'input-text size -L',
                                       }
                                   ), )
    def clean(self):
        print(self.cheaned_data, '%%%%%%%%%%%%%%%%%%%%')
        if not self.is_valid():
            raise forms.ValidationError(u'所有项都为必填项')
        elif self.cleaned_data['newpassword1'] != self.cleaned_data['newpassword2']:
            print('********************')
            raise forms.ValidationError('两次输入都不一样')
        else:
            cleaned_data = super(ChangePwdForm, self).clean()
        return cleaned_data
